package com.jiaquyun.jcps.ui.mine

import android.app.Activity
import android.content.Intent
import android.net.Uri
import androidx.lifecycle.Observer
import com.jiaquyun.jcps.R
import com.jiaquyun.jcps.base.ViewModeActivity
import com.jiaquyun.jcps.mode.vm.MineViewModel
import com.jiaquyun.jcps.popup.ConfirmCancelPopup
import com.lemlin.network.HttpServiceStatus
import kotlinx.android.synthetic.main.activity_today_income.*
import java.lang.Exception
import java.lang.StringBuilder

class TodayIncomeActivity : ViewModeActivity<MineViewModel>() {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, TodayIncomeActivity::class.java)
            activity.startActivity(intent)
        }
    }

    private val confirmCancelPopup: ConfirmCancelPopup by lazy {
        ConfirmCancelPopup(
            context = this,
            onConfirm = { any: Any?, action: ConfirmCancelPopup.Action? ->
                confirmCancelPopup.dismiss()
                if (action == null)
                    return@ConfirmCancelPopup
                if (action == ConfirmCancelPopup.Action.CONFIRM_SETTLE) {
                    viewModel.cashAdd()
                }
            },
            onCancel = { _: Any?, _: ConfirmCancelPopup.Action? ->
                confirmCancelPopup.dismiss()
            })
    }

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_today_income

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, color = R.color.colorWhite, view = flTodayIncomeBar)
    }


    override fun initView() {
        ivTodayIncomeBack.setOnClickListener {
            finish()
        }
        tvTodayIncomeSettle.setOnClickListener {
            viewModel.todayLiveData.value?.httpData?.data?.let {
                confirmCancelPopup.setMessage("提示：申请结算会将当前待结算金额（${it.user_money}元）全部结算，请确认无误后再操作")
                    .setAction(ConfirmCancelPopup.Action.CONFIRM_SETTLE)
                    .setAny(null)
                    .showPopupWindow()
            }
        }
    }

    override fun initData() {
        viewModel.todayData()
    }

    override fun initViewMode() {
        viewModel.todayLiveData.observe(this, Observer { response ->
            if (response.statusCode == HttpServiceStatus.Status.FAILURE)
                toast(response.statusTip)
            if (response.statusCode == HttpServiceStatus.Status.SUCCESS) {
                response.httpData?.data?.let {
                    tvTodayIncomeDeliveryPrice.text = StringBuilder().append("￥").append(it.money)
                    tvTodayIncomeNum.text = try {
                        it.num.toString()
                    } catch (e: Exception) {
                        "0"
                    }
                    tvTodayIncomeSettleEndPrice.text =
                        StringBuilder().append("￥").append(it.withdrawal_total_money)
                    tvTodayIncomeSettleStayPrice.text =
                        StringBuilder().append("￥").append(it.user_money)
                }
            }
        })

        viewModel.baseSettleLiveData.observe(this, Observer { response ->
            if (response.statusCode == HttpServiceStatus.Status.FAILURE)
                toast(response.statusTip)
            if (response.statusCode == HttpServiceStatus.Status.SUCCESS) {
                toast("结算完成")
                viewModel.todayData()
            }
        })
    }
}